const app = getApp();

// // 引入腾讯地图SDK核心类
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js')
// 实例化腾讯地图API核心类
var qqmapsdk = new QQMapWX({
  key: 'M6LBZ-ET6RJ-PU6FK-KDRWM-ZJO4E-35F64' // 必填
})

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    colorType: {
      // 组件颜色, dark为暗色系, light为浅色系
      type: String,
      value: 'dark'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    curTime: '',
    curDate: '',
    curLocation: '',
    curCity: '',
    weatherInfo: {
      curWeatherText: '',
      curWeatherIcon: '',
      curTemp: '0',
      maxTemp: '0',
      minTemp: '0',
    },
    isMorning: true,
  },
  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached: function () {
    /**
     * 初始化时钟模块数据历数据
     */
    this.initTime()
    this.initDate()
    // this.getWeather()
    this.getLocation()
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 初始化时间
    initTime() {
      let _this = this
      let hour = new Date().getHours()
      let isMorning = true
      if(hour >= 6 && hour <= 18) {
        isMorning = true
      } else {
        isMorning = false
      }
      _this.setData({
        isMorning
      })
      let currentTime = _this.formatTime(new Date())
      _this.setData({
        curTime: currentTime
      })
      setInterval(function() {
        let hour = new Date().getHours()
        let isMorning = true
        if(hour >= 6 && hour <= 18) {
          isMorning = true
        } else {
          isMorning = false
        }
        _this.setData({
          isMorning
        })
        let currentTime = _this.formatTime(new Date())
        _this.setData({
          curTime: currentTime
        })
      }, 10 * 1000)
    },
    formatTime(e) {
      let hour = ('0' + (e.getHours())).slice(-2)
      let minute = ('0' + (e.getMinutes())).slice(-2)
      let time = hour + ':' + minute
      return time
    },

    // 初始化日期
    initDate() {
      let handler = new Date()
      let month = handler.getMonth() + 1
      let day = handler.getDate()
      let week = this.formatWeek(handler.getDay())
      let str = month + '月' + day + '日' + week
      this.setData({
        curDate: str
      })
    },
    formatWeek(e) {
      switch(e) {
        case 0: {
          return '星期天'
        }
        case 1: {
          return '星期一'
        }
        case 2: {
          return '星期二'
        }
        case 3: {
          return '星期三'
        }
        case 4: {
          return '星期四'
        }
        case 5: {
          return '星期五'
        }
        case 6: {
          return '星期六'
        }
      }
    },

    // 初始化天气
    getWeather() {
      let _this = this
      // app._get('https://v0.yiketianqi.com/api?version=v9&appid=69884335&appsecret=Yb5HAAOO', {},
      app._get('https://tianqiapi.com/api?version=v6&appid=69884335&appsecret=Yb5HAAOO', {city: _this.data.curCity || ''},
        (res) => {
          let str = 'n_'
          if(_this.data.isMorning) {
            str = 'm_'
          }
          // iconn_yun
          /**
           * wea --天气  wea_img --天气icon
           * 多云       yun
           * 阴         yin
           * 晴         qing
           * 小雨转晴    yu
           * 雷         lei
           * 沙尘暴     shachen
           * 雾         wu
           * 冰雹       bingbao
           * 雪         xue
           */
          let weatherInfo = {}
          weatherInfo.curWeatherText = res.wea
          weatherInfo.curWeatherIcon = 'icon' + str + res.wea_img
          weatherInfo.curTemp = res.tem
          weatherInfo.maxTemp = res.tem1
          weatherInfo.minTemp = res.tem2
          this.setData({
            weatherInfo: weatherInfo
          })
        }, (err) => {
          // error callback
        }, (com) => {
          // complete callback
        })
    },

    // 获取地址
    getLocation() {
      let _this = this
      wx.getLocation({
        type: 'wgs84',
        success: function (res){
          let latitude = res.latitude // 纬度，浮点数，范围为90 ~ -90
          let longitude = res.longitude // 经度，浮点数，范围为180 ~ -180
          qqmapsdk.reverseGeocoder({
            location: {latitude: latitude,longitude: longitude},
            success: function (e) {
              let str = e.result.address_component.city
              let city = ''
              if(str.substr(str.length-1,1) === '市') {
                city = str.substr(0, str.length-1)
              } else {
                city = str
              }
              _this.setData({
                curLocation: e.result.address_component.district,
                curCity: city
              })
              // 获得地址授权则使用授权的城市信息
              _this.getWeather()
            }
          })
        },
        fail: function(err) {
          // 未获得地址授权则使用ip地址
          _this.getWeather()
        }
      })
    },
  }
})
